﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Dev101.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string ts = "03:44:32";
            TimeSpan resultTs = new TimeSpan();
            TimeSpan.TryParse(ts, out resultTs);
            Console.WriteLine(resultTs);
            
            var jsonString = File.ReadAllText(ConfigurationManager.AppSettings["DataDump"] + "tags.txt");
            var container = JsonConvert.DeserializeObject<Dev101.Models.TagContainer>(jsonString);
            var tagItemSource = container.tagsForCurrentPage.Where(t => t.name == "developer").First();
            //tagItem.courses = new List<Models.Source.Course>();
            //Dev101.Models.TagContainer tagContainer = new Models.TagContainer();
            //tagContainer.tagsForCurrentPage = new List<Models.Tag>();

            Dev101.Models.Tag tagItem = new Models.Tag();
                // Create a request for the URL. 
                WebRequest request = WebRequest.Create(
                  "http://data.pluralsight.com/Courses/Tags?id=developer&page=1&sort=new");
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                Console.WriteLine(responseFromServer);
                // Clean up the streams and the response.
                reader.Close();
                response.Close();
                tagItem = JsonConvert.DeserializeObject<Dev101.Models.Tag>(responseFromServer);
                tagItem.name = "developer";
                tagItemSource.courses = tagItem.courses;
            //var lt = JsonConvert.DeserializeObject<Dev101.Models.TagContainer>(responseFromServer);
            //foreach(var tag in lt.tagsForCurrentPage)
            //{
            //    Console.WriteLine("Tag:{0} / No of courses:{1}", tag.description, tag.numberOfCourses);
            //}

                jsonString = JsonConvert.SerializeObject(container);
                File.WriteAllText(ConfigurationManager.AppSettings["DataDump"] + "tags.txt", jsonString);

            //File.WriteAllText(ConfigurationManager.AppSettings["DataDump"] + "tags.txt", JsonConvert.SerializeObject(lt));
            
            /*
            string xlsxFile = ConfigurationManager.AppSettings["DataDump"] + "PersonalDevelopmentPlan v1.xlsx";
            using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ xlsxFile +@";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"))
            {
                try
                {
                    Console.WriteLine("We are open");
                    conn.Open();
                    DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    List<string> sheetTables = new List<string>();
                    foreach (DataRow t in schemaTable.Rows)
                    {
                        foreach (DataColumn col in schemaTable.Columns)
                        {
                            Console.WriteLine(t[col]);
                            sheetTables.Add(t[col].ToString());
                        }
                    }
                    foreach (var s in sheetTables)
                    {
                        DataTable dt = new DataTable();
                        string cmdText = "SELECT * FROM " + "[" + s + "]";
                        OleDbCommand cmd = new OleDbCommand(cmdText);
                        cmd.Connection = conn;
                        OleDbDataReader reader = cmd.ExecuteReader();
                        dt.Load(reader);
                        dt.TableName = s.Trim('$');
                        //data.Add(dt);
                    }
                }
                catch (OleDbException exception)
                {
                    Console.WriteLine(exception.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            */
        }
    }
}
